<?php
use Cribbb\Storage\Category\CategoryRepository as Category;

class AdminCategoryController extends \BaseController {

	protected $layout = 'admin/layout/default';
	
	protected $category;
	
	public function __construct(Category $category)
	{
		$this->category = $category;
	}
	
	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$categories = $this->category->all();
	
		$this->layout->content = View::make('admin.category.index')->with('categories', $categories);
	}

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		$this->layout->content = View::make('admin.category.create');
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{		
// 		$validator = Validator::make(Input::all(), Category::$rules, Category::$messages);
		
// 		if ($validator->fails()) {
// 			return Redirect::to('admin/category/create')
// 				->withErrors($validator)
// 				->withInput();
// 		} else {
// 			Category::create(Input::all());
		
// 			Session::flash('message', 'Successfully created Category!');
// 			return Redirect::to('admin/category');
// 		}

		$validator = new Cribbb\Services\Validators\Category;
		
		if ($validator->passes())
		{
			$this->category->create(Input::all());
			
			Session::flash('message', 'Successfully created Category!');
			return Redirect::route('admin.category.index');
		}
		
		return Redirect::route('admin.category.create')
			->withInput()
			->withErrors($validator->getErrors());
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		return $category = $this->category->find($id);

		$this->layout->content = View::make('admin.category.show')->with('category', $category);
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		$category = $this->category->find($id);

		$this->layout->content = View::make('admin.category.edit')->with('category', $category);
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		$validator = new Cribbb\Services\Validators\Category;
		
		if ($validator->passes()) {
			$this->category->update($id, Input::all());
				
			Session::flash('message', 'Successfully updated Category!');
			return Redirect::to('admin/category');
		}
		
 		//return Redirect::to('admin/category/' . $id . '/edit')
 		return Redirect::route('admin.category.edit', $id)
 				->withErrors($validator->getErrors())
 				->withInput();
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		$this->category->delete($id);

		Session::flash('message', 'Successfully deleted the Category!');
		return Redirect::to('admin/category');
	}
}